// pages/bindTel/bindTel.js
// import WxValidate from '../../utils/WxValidate.js';
var app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    buttonText:'获取验证码',
    tel:'',
    disabled:false,
    code:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // this.initValidate();
    wx.hideShareMenu()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  getCode:function(){
    let phone = this.data.tel;
    let self = this;
   


    if (phone.match(/^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$/)) {
      let num = 61;
      var timer = setInterval(function () {
        num--;
        if (num <= 0) {
          clearInterval(timer);
          self.setData({
            buttonText: '重新发送',
            disabled: false
          })
        } else {
          self.setData({
            buttonText: num + "s后重新发送",
            disabled: true
          })
        }
      }, 1000)
      wx.request({
        url: app.globalData.reqPath + 'shop/MembersSms',
        header: {
          'authorization': app.globalData.authorization
        },
        data: {
          phone: self.data.tel
        },
        method: "POST",
        success: function (res) {   
          
          if(res.data.code=200){
            let authorization=res.header.authorization||res.header.Authorization;
           // console.log("auth1:" + authorization)
            app.globalData.authorization=authorization
            wx.setStorage({
              key: 'authorization',
              data: authorization,
            })
          }
        }

      })
    } else {
      wx.showToast({
        title: '请输入正确的手机号',
        icon: 'none'
      })
    }
  },
  inputTel:function(e){
    this.setData({tel: e.detail.value})
  },
  inputcode:function(e){
    this.setData({ code: e.detail.value })
  },
  subBind:function(e){
   // console.log(e.detail.value)
    let parames = e.detail.value
    // if (!this.WxValidate.checkForm(parames)) {
    //   const error = this.WxValidate.errorList[0]
    //   this.showModal(error)
    //   return false
    // } 
    if (parames.vericode == '') {
      wx.showToast({
        title: '请输入验证码',
        icon:'none'
      })
     } else if (parames.tel==''){
      wx.showToast({
        title: '请输入手机号码',
        icon: 'none'
      })
    }else
    if (parames.vericode != '' && parames.tel!=''){
      let data = {
        code: parames.vericode,
        phone: parames.tel
      }
      //console.log(data)
      //console.log("auth3:" + app.globalData.authorization)
      wx.request({
        url: app.globalData.reqPath + 'base/BindPhone',
        method: 'POST',
        header: {
          'authorization': app.globalData.authorization
        },
        data: JSON.stringify(data),
        success: function (res){
         // console.log("res2:" + JSON.stringify(res))
          if (res.data.code == 200) {
            let authorization = res.header.authorization || res.header.Authorization
            app.globalData.authorization=authorization;
            wx.setStorage({
              key: 'authorization',
              data: authorization,
            })
            wx.showToast({
              title: '绑定成功',
            })
            //获取个人信息
            wx.request({
              url: app.globalData.reqPath + 'shop/Members',
              data: {},
              header: {
                'authorization': app.globalData.authorization
              },
              success: function (res) {
                console.log("个人信息：" + JSON.stringify(res))
                if (res.data.code == 200) {
                  wx.setStorage({
                    key: 'userInfo',
                    data: res.data.data,
                  })

                }
              }
            })

            setTimeout(function () {             
                wx.switchTab({
                  url: '/pages/index/index',
                })
            }, 500)
          } else {
            wx.showToast({
              title: res.data.data.msg,
              icon: 'none'
            })
          }
        }
      })
    }
  },
  // initValidate() {
  //   const rules = {
  //     tel: {
  //       required: true,
  //       tel: true
  //     },
  //     vericode: {
  //       required: true,
  //       number: true,
  //       maxlength: 8
  //     }
  //   }
  //   const messages = {
  //     tel: {
  //       required: '请输入手机号',
  //       tel: '请输入正确的手机号'
  //     },
  //     vericode: {
  //       required: "请输入验证码",
  //       number: "请输入正确的验证码",
  //       maxlength: '请输入正确的验证码'
  //     }
  //   }
  //   this.WxValidate = new WxValidate(rules, messages)
  // },
  // showModal: function (error) {
  //   wx.showToast({
  //     title: error.msg,
  //     icon:none
  //   })
  // }
})